package com.enqualcomm.kids.mvp.reai;

import android.net.Uri;
import android.support.v4.util.SimpleArrayMap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.bean.TerminalIcon;
import com.enqualcomm.kids.config.ImageUriFactory;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import common.utils.MyLogger;

/**
 * Created by yinzhiqun on 2016/1/26.
 */
public class TerminalHeaderAdapter extends RecyclerView.Adapter<TerminalHeaderAdapter.ViewHolder> {

    LayoutInflater inflater;

    List<TerminalIcon> imagesCopy;
    /**
     * key:头像地址，value:名字
     */
    List<TerminalIcon> images;

    public TerminalHeaderAdapter(LayoutInflater inflater, List<TerminalIcon> images) {
        this.inflater = inflater;
        this.images = images;
    }

    public void changeImages(List<TerminalIcon> images){
        this.images = images;
    }


    @Override
    public TerminalHeaderAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_reai_terminal, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(TerminalHeaderAdapter.ViewHolder holder, int position) {
        final TerminalIcon terminalIcon = imagesCopy.get(position);
        Uri uri = ImageUriFactory.getFileUri(terminalIcon.imagePath);
        holder.simpleDraweeView.setImageURI(uri);
        holder.name_tv.setText(terminalIcon.terminalName);
    }

    @Override
    public int getItemCount() {
        return imagesCopy ==null?0: imagesCopy.size();
    }

    public void setSelectedItem(int position) {
        imagesCopy = new ArrayList<>();
        for (int i = 0,size = images.size(); i < size; i++) {
            if (i!=position){
                imagesCopy.add(images.get(i));
            }
        }
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        public SimpleDraweeView simpleDraweeView;
        public TextView name_tv;
        public ViewHolder(View itemView) {
            super(itemView);
            this.simpleDraweeView = (SimpleDraweeView) itemView.findViewById(R.id.header_iv);
            this.name_tv = (TextView) itemView.findViewById(R.id.name_tv);
        }
    }
}
